﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using BLL;
using Model;
using Common;

namespace API.Controllers
{
    [RoutePrefix("api/AdminUser")]
    public class AdminUserController : ApiController
    {
        B_AdminsUser _bau;
        public AdminUserController()
        {
            _bau = new B_AdminsUser();
        }

        /// <summary>
        /// 获取所有管理员(有参数的)
        /// </summary>
        /// <returns></returns>
        [Route("GetData")]
        [HttpGet]
        public IHttpActionResult GetData(int page, int size)
        {
            var data = from i in _bau.GetAll()
                       select new AdminUser
                       {
                           AdminID = i.AdminID,
                           AdminName = i.AdminName,
                           PassWord = i.PassWord,
                           NickName = i.NickName,
                           HeadImg = i.HeadImg,
                           CreateTime = i.CreateTime,
                           IsEnable = i.IsEnable,
                           Power = i.Power
                       };
            return Ok(new Result<List<AdminUser>>
            {
                Data = data.Skip((page - 1) * size).Take(size).ToList(),
                Msg = data.Count().ToString()
            });
        }
        /// <summary>
        /// 获取所有管理员
        /// </summary>
        /// <returns></returns>
        [Route("GetData")]
        [HttpGet]
        public IHttpActionResult GetData()
        {
            var data = from i in _bau.GetAll()
                       select new AdminUser
                       {
                           AdminID = i.AdminID,
                           AdminName = i.AdminName,
                           PassWord = i.PassWord,
                           NickName = i.NickName,
                           HeadImg = i.HeadImg,
                           CreateTime = i.CreateTime,
                           IsEnable = i.IsEnable,
                           Power = i.Power
                       };
            return Ok(new Result<List<AdminUser>> { Data = data.ToList() });
        }

        /// <summary>
        /// 添加管理员
        /// </summary>
        /// <param name="admin"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("Add")]
        public IHttpActionResult Add(AdminUser admin)
        {
            var data = HttpHelper.HttpPost<Result<string>>(Tools.ImgPath, "post", new
            {
                base64 = admin.HeadImg,
                imgurl = "HeadImg",
            });
            if (data.Code == 200)
            {
                try
                {
                    //添加这样写是为了图片
                    var aa = new AdminUser
                    {
                        AdminID = admin.AdminID,
                        AdminName = admin.AdminName,
                        PassWord = admin.PassWord,
                        NickName = admin.NickName,
                        HeadImg = data.Data,//图片
                        CreateTime = DateTime.Now,
                        IsEnable = admin.IsEnable,
                        Power = admin.Power
                    };
                    _bau.Add(aa);
                    return Ok(new Result<object>());
                }
                catch (Exception)
                {
                    return Ok(new Result<object> { Code = 404, Msg = "出现错误，请联系管理员" });
                }
            }
            return Ok(new Result<object> { Code = 404, Msg = "出现错误，请联系管理员" });
        }

        /// <summary>
        /// 删除管理员
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("Delete/{id}")]
        public IHttpActionResult Delete(int id)
        {
            try
            {
                _bau.Delete(id);
                return Ok(new Result<object>
                {
                    Code = 200,
                    Msg = "删除成功"
                });
            }
            catch (Exception)
            {
                return Ok(new Result<object>
                {
                    Code = 500,
                    Msg = "该用户已删除"
                });
            }
        }

        /// <summary>
        /// 查询管理员详情
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns></returns>
        [Route("Find/{id}")]
        [HttpGet]
        public IHttpActionResult Find(int id)
        {
            try
            {
                var data = _bau.FindById(id);
                if (data == null)
                {
                    return Ok(new Result<object>
                    {
                        Code = 404,
                        Msg = "数据走丢了"
                    });
                }
                return Ok(new Result<Param_AdminUser>
                {
                    Data = new Param_AdminUser
                    {
                        AdminID = data.AdminID,
                        AdminName = data.AdminName,
                        PassWord = data.PassWord,
                        NickName = data.NickName,
                        HeadImg = data.HeadImg,
                        CreateTime = DateTime.Now,
                        IsEnable = data.IsEnable,
                        Power = data.Power
                    }
                });
            }
            catch (Exception exp)
            {
                return Ok(new Result<object>
                {
                    Code = 500,
                    Msg = exp.Message
                });
            }
        }

        /// <summary>
        /// 更新管理员
        /// </summary>
        /// <param name="id"></param>
        /// <param name="admin"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("Update/{id}")]
        public IHttpActionResult Update(int id, Param_AdminUser admin)
        {
            //var result = b.FindById(id);&&result.Power.Value
            var data = HttpHelper.HttpPost<Result<string>>(Tools.ImgPath, "post", new
            {
                base64 = admin.HeadImg,
                imgurl = "HeadImg",
            });
            if (data.Code == 200)
            {
                var aa = new AdminUser
                {

                    AdminName = admin.AdminName,
                    PassWord = admin.PassWord,
                    NickName = admin.NickName,
                    HeadImg = data.Data,//图片
                    CreateTime = DateTime.Now.AddDays(-1),
                    IsEnable = admin.IsEnable,
                    Power = admin.Power,
                };
                _bau.Update(id, aa);
                return Ok(new Result<object>());
            }
            return Ok(new Result<object> { Code = 404, Msg = "不是超管，不能改" });
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="admin"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("Login")]
        public IHttpActionResult Login([FromBody]Param_Login admin)
        {
            var data = _bau.Login(admin.Username, admin.Password);
            if (data != null)
            {
                return Ok(new Result<object>
                {
                    Data = data
                });
            }
            else
            {
                return Ok(new Result<object>
                {
                    Code = 404,
                    Msg = "用户不存在"
                });
            }
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="id"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        [Route("UpPass/{id}")]
        [HttpPost]
        public IHttpActionResult UpPass(int id, Param_UpPass param)
        {
            if (_bau.FindById(id).PassWord==param.OldPassWord)
            {
                var data = _bau.UpPass(id, param.PassWord);
                if (data == 0)
                {
                    return Ok(new Result<object>() { Code = 404, Msg = "该管理员不存在" });
                }
                return Ok(new Result<object>());
            }
            else
            {
                return Ok(new Result<object>() { Code = 500, Msg = "原密码不正确" });
            }
        }
    }
}
